import fabric from 'fabric';
import scaleImage from '../assets/images/scale.png';
import rotateImage from '../assets/images/rotate.png';
import mirrorImage from '../assets/images/mirror.png';

/* eslint-disable */

const isVML = function() { return typeof G_vmlCanvasManager !== 'undefined'; };
fabric.util.object.extend(fabric.Object.prototype, {
    hasRotatingPoint: true,
    cornerSize: 22,
    _drawControl: function(control, ctx, methodName, left, top) {
        if (!this.isControlVisible(control)) {
            return;
        }
        var size = this.cornerSize;
        isVML() || this.transparentCorners || ctx.clearRect(left, top, size, size);

        if (control !== 'tl' && control !== 'br' && control !== 'bl' && control !== 'tr') ctx['fillRect'](left, top, size, size);
        var SelectedIconImage = new Image();
        if (control === 'tl' || control === 'br') {
            SelectedIconImage.src = rotateImage;
            ctx.drawImage(SelectedIconImage, left, top, size, size);
        }
        if (control === 'tr') {
            SelectedIconImage.src = scaleImage;
            ctx.drawImage(SelectedIconImage, left, top, size, size);
        }
        if (control === 'bl') {
            SelectedIconImage.src = mirrorImage;
            ctx.drawImage(SelectedIconImage, left, top, size, size);
        }
    }
});
const cursorOffset = {
    mt: 0, // n
    tr: 1, // ne
    mr: 2, // e
    br: 3, // se
    mb: 4, // s
    bl: 5, // sw
    ml: 6 // w
};
let degreesToRadians = fabric.util.degreesToRadians;
fabric.util.object.extend(fabric.Canvas.prototype, {
    setCursor: function (value) {
        this.upperCanvasEl.style.cursor = value;
    },
    _getActionFromCorner: function(target, corner) {
        var action = 'drag';
        if (corner && corner !== 'bl') {
            action = (corner === 'ml' || corner === 'mr') ?
            'scaleX' : (corner === 'mt' || corner === 'mb') ?
            'scaleY' : (corner === 'mtr' || corner === 'tl' || corner === 'br') ?
            'rotate' : 'scale';
            return action;
        }
        if (corner === 'bl') {
            target.flipX = !target.flipX;
            this.renderAll();
        }
    },
    _setCornerCursor: function(corner, target) {
        if ((corner === 'mtr' || corner === 'tl') && target.hasRotatingPoint) {
            this.setCursor(this.rotationCursor);
        } else if (corner in cursorOffset) {
            this.setCursor(this._getRotatedCornerCursor(corner, target));
        } else {
            this.setCursor(this.defaultCursor);
            return false;
        }
    }
});

/* eslint-enable */
